﻿using System;
using System.Collections.Generic;
using System.IO;
using Ionic.Zip;

namespace Furesoft.Addins
{
    public class RuntimeAddin
    {
        public Manifest Manifest { get; set; }

        private List<ZipEntry> entries;

        public MemoryStream GetEntry(string name)
        {
            MemoryStream ms = new MemoryStream();

            foreach (var item in entries)
            {
                if (item.FileName == name)
                {
                    item.Extract(ms);
                }
            }

            return ms;
        }

        public RuntimeFolder GetFolder(string name)
        {
            var rf = new RuntimeFolder();

            foreach (var e in entries)
            {
                if (e.FileName == name)
                {
                    if (e.IsDirectory)
                    {
                        //ToDo: implement
                    }
                }
            }

            return rf;
        }

        public RuntimeAddin()
        {
            entries = new List<ZipEntry>();
        }

        public static RuntimeAddin Open(string filename)
        {
            var ret = new RuntimeAddin();

            using (var zip = ZipFile.Read(filename))
            {
                foreach (var e in zip.Entries)
                {
                    ret.entries.Add(e);
                }
            }

            return ret;
        }

    }
}